package com.crtf.hello.kotlin.basic.syntax

import java.io.FileInputStream
import java.util.*
import java.util.concurrent.ThreadLocalRandom


open class BasicSyntax {


    // 定义函数
    fun sum(a: Int, b: Int): Int {
        return a + b
    }

    // 定义局部变量
    fun definingLocalVariables(): Unit {
        // 只读局部变量
        val a: Int = 1
        val b = 1
        val c: Int
        c = 1

        // 可变变量
        var x = 5
        x += 1
    }

    // 可选字段
    fun optionalFields(a: Unit): Unit {
        val email: String?
        val email2: String? = null
        val email3: String = "value"
    }

    // 类
    class ItemManager(val categoryId: String, val dbConnection: String) {
        constructor(categoryId: String, dbConnection: String, email: String)
                : this(categoryId, dbConnection) {
            this.email = email
        }

        var email = ""

        // ...
        fun isFromSpecificCategory(catId: String): Boolean {
            return categoryId == catId
        }
    }

    // 继承
    open class Item(val id: String, val name: String = "unknown_name") {
        open fun getIdOfItem(): String {
            return id
        }
    }

    class ItemWithCategory(id: String, name: String, val categoryId: String) : Item(id, name) {
        override fun getIdOfItem(): String {
            return id + name
        }
    }

    // 条件语句
    fun makeAnalyisOfCategory(catId: String): Unit {
        val result = if (catId == "100") "Yes" else "No"
        println(result)

        val number = 2
        if (number < 10) {
            println("number less that 10")
        } else if (number > 10) {
            println("number is greater that 10")
        }

        val name = "John"
        when (name) {
            "John" -> println("Hi man")
            "Alice" -> println("Hi lady")
        }

    }

    // 集合
    fun collections() {
        // 可变和不可变
        val listItems = listOf(1, 2, 3, 4)
        val mutableListItem = mutableListOf(1, 2, 3)
        mutableListItem.add(5)

        val setItem = setOf(1, 2)
        val mutableSetItem = mutableSetOf(1, 2, 2)
        mutableSetItem.add(1)

        val mapItem = mapOf("k1" to "v1", "k2" to "v2")
        val mutableMapItem = mutableMapOf("k1" to "v1", "k2" to "v2")
        mutableMapItem.put("k1", "v1")
    }

    // 异常处理
    fun exceptions() {
        try {
            throw Exception("msg")
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {

        }
    }

    // Lambdas
    fun lambdas() {
        val sumLambda = { a: Int, b: Int -> a + b }

        // given
        val listOfNumbers = listOf(1, 2, 3)

        // when
        val sum = listOfNumbers.reduce { a, b -> a + b }
    }

    // 循环结构
    fun loopingConstructs() {
        val numbers = arrayOf("first", "second", "third", "fourth")
        for (n in numbers) {
            println(n)
        }

        for (i in 2..9 step 2) {
            println(i)
        }

        1.rangeTo(10).map { it * 2 }
    }

    // 空安全
    fun nullSafety(id: String): Item? {
        val itemId = UUID.randomUUID().toString()
        return Item(itemId, "name-$itemId")
    }

    // 数据类
    data class DataItem(val id: String, val name: String = "unknown_name")

    // 扩展功能
    fun extensionFunctions() {
        val elements = listOf("a", "b", "c")
        val kFunction0 = elements random 3
    }

    private infix fun <T> List<T>.random(max: Int): T? {
        if (this.isEmpty()) return null
        return get(ThreadLocalRandom.current().nextInt(if (count() > max) max else count()))
    }

    // 字符串模板
    fun stringTemplates() {
        val firstName = "Tom"
        val secondName = "Mary"
        val concatOfNames = "$firstName + $secondName"
        val sum = "four: ${2 + 2}"

        val itemManager = ItemManager("cat_id", "db://connection")
        val result = "function result: ${itemManager.isFromSpecificCategory("1")}"
    }

    // Kotlin/Java 互操作性
    fun kotlinJavaInteroperability() {
        ThreadLocalRandom.current().nextInt()
        `Objects`.isNull(null)
    }

}
